<template>
	<view>
		<view class="container">
			<mescroll-body  @init="mescrollInit" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback">
				<view class="easy-item" v-for="(item, index) in noteList" :key="index">
					<!-- <view class="avatar"><image :src="item.clientUserLogo" mode="widthFix" @tap="goToUsers(item.clientUserId)"></image></view> -->
					<view class="avatar"><image :src="item.clientUserLogo" mode="widthFix"></image></view>
					<view class="content">
						<text class="nick-name">{{ item.clientUserName }}</text>

						<text class="content-text" selectable="true">{{ item.contentText }}</text>

						<view class="thumbnails" v-if="item.discussImages && item.discussImages.length > 0">
							<view :class="item.discussImages.length === 1 ? 'my-gallery' : 'thumbnail'" v-for="(image, index_images) in item.discussImages" :key="index_images">
								<image class="gallery_img" lazy-load mode="aspectFill" :src="image" :data-src="image" @tap="previewImage(item.discussImages, index_images)"></image>
							</view>
						</view>
						<view class="sub">
							<text class="datetime">{{ item.creationTimeStr }}</text>
							<!-- <view class="operation">
								<view class="mutiple" @tap="upItem(item.id, index, item.isStar)">
									<fa-icon class="icon" type="thumbs-o-up" size="22" v-if="!item.isStar"></fa-icon>
									<fa-icon class="icon" type="thumbs-up" size="22" v-else color="#DC3545"></fa-icon>
								</view>
								<view class="mutiple" @tap="comment(item.id, index)">
									评论
								</view>
							</view> -->
						</view>
						<view class="sub" v-if="item.canDelete || item.own">
							<view></view>
							<view class="operation single"><view @tap="deleteItem(item.id, index)">删除</view></view>
						</view>
						<view class="comment">
							<view class="top-arrow" v-if="item.discussStars.length > 0 || item.discussReplies.length > 0"></view>
							<view v-if="item.discussStars.length > 0">
								<view class="up-list">
									<fa-icon class="icon" type="heart-o" size="20" v-if="!item.isStar"></fa-icon>
									<fa-icon class="icon" type="heart" size="20" v-else color="#DC3545"></fa-icon>
									<block v-for="(up_item, up_index) in item.discussStars" :key="up_index">
										<text class="up-item">{{ up_item.clientUserName }}</text>
									</block>
								</view>
							</view>
							<view class="comment-list" v-if="item.discussReplies.length > 0">
								<view class="comment-item" v-for="(comment_item, comment_index) in item.discussReplies" :key="comment_index">
									<text class="comment-name">{{ comment_item.clientUserName }}:</text>
									<text class="comment-content">{{ comment_item.contentText }}</text>
									<view class="comment-delete">
										<text
											class="comment-delete-btn"
											v-if="comment_item.canDelete || comment_item.own"
											@tap="commentDelete(comment_item.id, comment_index, index)"
										>
											删除
										</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>			
				<mescroll-empty v-if="noteList.length==0"></mescroll-empty>
			</mescroll-body>
			<view class="foot" v-show="showInput">
				<chat-input @send-message="send_comment" :show="showInput" @blur="blur" :focus="focus" :placeholder="input_placeholder"></chat-input>
			</view>
		</view>
	</view>
</template>

<script>
import yxylSwiper from '@/components/yxyl-swiper/yxyl-swiper.vue';
import faIcon from '@/components/kilvn-fa-icon/fa-icon.vue';
import MescrollBody from 'mescroll-uni/mescroll-body.vue'
import MescrollMixin from "mescroll-uni/mescroll-mixins.js";
import MescrollEmpty from 'mescroll-uni/components/mescroll-empty.vue';
import chatInput from '@/components/im-chat/chatinput.vue';
export default {
	mixins: [MescrollMixin],
	components: {
		faIcon,
		MescrollBody,
		chatInput,
		MescrollEmpty
	},
	data() {
		return {
			swiperItems: [],
			noteList: [],
			noteListIndex: -1,

			// 下拉刷新的常用配置
			downOption: {
				use: true, // 是否启用下拉刷新; 默认true,
				auto: false,
				native:true
			},
			//上拉加载的常用配置
			upOption: {
				use: true, // 是否启用上拉加载; 默认true
				auto: false, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
				noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
				empty: false,
				textNoMore: '---没有了---',
				toTop: {
					bottom: 250
				}
			},
			input_placeholder: '评论',
			is_reply: false, //回复还是评论
			showInput: false, //评论输入框
			focus: false, //是否自动聚焦输入框
			discussId: '',
			fabPattern: {
				buttonColor: '#F44336'
			},
			clientUserId: '',
			discussGroupId: '',
			labelId: ''
		};
	},
	onLoad(e) {
		// #ifndef H5
		const updateManager = uni.getUpdateManager();
		updateManager.onUpdateReady(function(res) {
			uni.showModal({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate();
					}
				}
			});
		});
		// #endif

		var that = this;
		if (e.clientUserId) {
			this.clientUserId = e.clientUserId;
		}
		if (e.discussGroupId) {
			this.discussGroupId = e.discussGroupId;
		}
		if (e.labelId) {
			this.labelId = e.labelId;
		}
		this.bindList().then(() => {
			// #ifdef H5
			that.$wechat.hideMenu();
			// #endif
		});
	},
	computed: {
		isOwn() {
			return this.clientUserId == this.$store.state.user.clientUserId;
		}
	},
	onShow() {},	
	methods: {
		goToUsers(clientUserId) {
			if (clientUserId) {
				uni.navigateTo({
					url: 'discuss?clientUserId=' + clientUserId
				});
			}
		},		
		previewImage(imageList, image_index) {
			var current = imageList[image_index];
			var urls = new Array();
			imageList.forEach(item => {
				urls.push(item);
			});
			uni.previewImage({
				current: current,
				urls: urls
			});
		},
		upItem(discussId, index, isStar) {
			if (!isStar) {
				this.$http
					.request({
						url: 'api/Discuss/CreateDiscussStar',
						data: {
							discussId: discussId
						},
						method: 'PUT'
					})
					.then(res => {
						this.noteList[index].isStar = true;
						this.noteList[index].discussStars.push(res);
					});
			}
		},
		downCallback() {
			this.noteList = [];
			this.bindList()
				.then(() => {
					this.mescroll.endSuccess();
				})
				.catch(() => {
					this.mescroll.endErr();
				});
		},
		async upCallback(page) {
			try {
				var Count = await this.bindList();
				this.mescroll.endSuccess(Count, Count > 0);
			} catch (e) {
				// 失败隐藏下拉加载状态
				this.mescroll.endErr();
			}
		},
		blur: function() {
			this.init_input();
		},
		init_input() {
			this.showInput = false;
			this.focus = false;
			this.input_placeholder = '评论';
			this.is_reply = false;
		},
		comment(id, index) {
			this.showInput = true; //调起input框
			this.focus = true;
			this.noteListIndex = index;
			this.discussId = id;
		},
		send_comment(message) {
			var that = this;

			this.init_input();
			this.$http
				.request({
					url: 'api/Discuss/CreateDiscussReply',
					method: 'PUT',
					data: {
						discussId: that.discussId,
						contentText: message.content,
						isAdmin: false
					}
				})
				.then(res => {
					that.noteList[that.noteListIndex].discussReplies.push(res);
				})
				.finally(() => {
					that.discussId = '';
				});
		},
		deleteItem(id, index) {
			var that = this;
			uni.showModal({
				title: '确认删除吗？',
				success: function(res) {
					if (res.confirm) {
						that.noteList.splice(index, 1);
						that.noteList = that.noteList;
						that.$http.request({
							url: 'api/Discuss/DeleteDiscuss',
							method: 'DELETE',
							data: {
								id: id
							}
						});
					}
				}
			});
		},
		commentDelete(id, commentIndex, index) {
			var that = this;
			uni.showModal({
				title: '确认删除吗?',
				success: function(res) {
					if (res.confirm) {
						that.noteList[index].discussReplies.splice(commentIndex, 1);
						that.noteList = that.noteList;
						that.$http.request({
							url: 'api/Discuss/DeleteDiscussReply',
							method: 'DELETE',
							data: {
								id: id
							}
						});
					}
				}
			});
		},
		async bindList() {
			var that = this;
			var result = await this.$http.request({
				url: 'api/Discuss/GetAllDiscuss',
				data: {
					LabelId: this.labelId,
					DiscussGroupId: this.discussGroupId,
					ClientUserId: this.clientUserId,
					SkipCount: this.noteList.length,
					MaxResultCount: 30
				}
			});
			if (!this.noteList.length) {
				uni.setNavigationBarTitle({
					title: result.title
				});
			}
			this.noteList = this.noteList.concat(result.items);
			return result.items.length;
		}
	}
};
</script>

<style lang="scss" scoped>
.easy-item {
	width: 95%;
	margin: 0 auto;
	display: flex;
	padding: 20rpx 0;

	.avatar {
		flex-basis: 15vw;
		flex-shrink: 0;
		flex-grow: 0;
		image {
			width: 15vw;
			height: 15vw;
			border-radius: 15px;
		}
	}
	.content {
		flex-basis: 75vw;
		display: flex;
		flex-direction: column;
		flex-shrink: 0;
		flex-grow: 0;
		padding-left: 15rpx;
		.nick-name {
			font-weight: 600;
			color: #6699cc;
			font-size: 42rpx;
		}
		.content-text {
			width: 100%;
			margin-top: 10rpx;
			font-size: 38rpx;
			line-height: 56rpx;
		}
	}
	.thumbnails {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		margin-top: 20rpx;
		.my-gallery {
			width: 100%;
			.gallery_img {
				width: 100%;
			}
		}
		.thumbnail {
			flex-basis: 22vw;
			height: 22vw;
			flex-grow: 0;
			flex-shrink: 0;
			padding-left: 10rpx;
			padding-bottom: 10rpx;
			.gallery_img {
				width: 100%;
				height: 100%;
			}
		}
	}
	.sub {
		width: 100%;
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
		.datetime {
			font-size: 24rpx;
			color: #c0c0c0;
			height: 50rpx;
			line-height: 50rpx;
		}
		.operation {
			font-size: 28rpx;
			color: #6699cc;
			display: flex;
			align-items: center;
			flex-basis: 50%;
			flex-shrink: 0;
			flex-grow: 0;
			&.single {
				justify-content: flex-end;
			}
			.delete {
				margin-right: 20rpx;
			}
			.mutiple {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				background: #fff;
				text-align: center;
				font-weight: 800;
				border: 1px solid #ebedf0;
				padding: 5rpx 20rpx;
				border-radius: 10rpx;
				color: #999999;
				flex-basis: 40%;
				flex-grow: 0;
				margin-left: 10rpx;
				height: 50rpx;
				font-size: 25rpx;
				.icon {
					margin-right: 10rpx;
				}
			}
		}
	}
	.comment {
		.top-arrow {
			width: 0;
			height: 0;
			border: 20rpx solid transparent;
			border-bottom-color: #eeeeee;
			margin-left: 20rpx;
		}
		.up-list {
			width: 100%;
			background: #eeeeee;
			padding: 10rpx;
			display: flex;
			flex-wrap: wrap;
			padding-bottom: 10rpx;
			border-bottom: 1px solid #dddddd;
			.up-item {
				color: #607d8b;
				font-size: 30rpx;
				padding-left: 10rpx;
				font-weight: 600;
				line-height: 50rpx;
				height: 50rpx;
				&:after {
					content: ',';
				}
				&:last-child:after {
					content: '';
				}
			}
		}
		.comment-list {
			width: 100%;
			background: #eeeeee;
			padding: 10rpx;

			.comment-item {
				width: 100%;
				.comment-name {
					color: #496f94;
					font-size: 32rpx;
				}
				.comment-content {
					font-size: 32rpx;
					line-height: 44rpx;
				}
				.comment-delete {
					width: 100%;
					display: flex;
					justify-content: flex-end;
					.comment-delete-btn {
						font-size: 28rpx;
						color: #6699cc;
						margin-right: 20rpx;
					}
				}
			}
		}
	}
}
.foot {
	position: fixed;
	bottom: var(--window-bottom);
	z-index: 9999;
	width: 100%;
}
.add-btn {
	font-size: 40px;
	color: #fff;
	display: flex;
	justify-content: center;
	align-items: center;
}
.gotoOwn {
	color: #ffffff;
}
</style>
